package com.example.xyr.myapplication.utils;

import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.model.LatLng;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by ylcf on 2018/8/15.
 * 高德地图位置搜索工具类
 */

public class LatlonUtils implements AMapLocationListener, GeocodeSearch.OnGeocodeSearchListener {

    private static LatlonUtils latlonUtils;

    private MapLocatino mapLocatino;
    private GeocodeSearch geocodeSearch;
    private LatLng latLng;
    private String address;
    private String otherAddress;


    private LatlonUtils(Context context) {
        //地理搜索类
        geocodeSearch = new GeocodeSearch(context);
        geocodeSearch.setOnGeocodeSearchListener(this);
    }

    public static LatlonUtils getInstance(Context context) {
        if (latlonUtils == null) {
            latlonUtils = new LatlonUtils(context);
        }
        return latlonUtils;
    }

    public void setMapLocatno(MapLocatino mapLocatino) {
        this.mapLocatino = mapLocatino;
    }

    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
        RegeocodeAddress regeocodeAddress = regeocodeResult.getRegeocodeAddress();
        String formatAddress = regeocodeAddress.getFormatAddress();
        otherAddress = formatAddress.substring(9);
        LogUtil.e("tag", "位置查询成功》》》》》》》》》》" + otherAddress);
        mapLocatino.onLocationChanged(otherAddress);
    }

    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {

    }

    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (amapLocation != null) {
            if (amapLocation.getErrorCode() == 0) {
                //定位成功回调信息，设置相关消息
                amapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见定位类型表
                double latitude = amapLocation.getLatitude();//获取纬度
                double longitude = amapLocation.getLongitude();//获取经度
                amapLocation.getAccuracy();//获取精度信息
                address = amapLocation.getAddress();
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date(amapLocation.getTime());
                df.format(date);//定位时间
                latLng = new LatLng(latitude, longitude);
            } else {
                //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                LogUtil.e("AmapError", "location Error, ErrCode:"
                        + amapLocation.getErrorCode() + ", errInfo:"
                        + amapLocation.getErrorInfo());
            }
        }
    }

    public LatLng getLatlng() {
        return latLng;
    }

    public String getAddress() {
        return address;
    }

    public String getOtherAddress() {
        return otherAddress;
    }

    public void getStrAddress(String userAddress, MapLocatino mapLocatino) {
        this.mapLocatino = mapLocatino;
        String[] split = userAddress.split("&");
        if (split.length > 1) {
            double d = Double.valueOf(split[0]);
            double d1 = Double.valueOf(split[1]);
            LatLng oLatlng = new LatLng(d, d1);
            getAddressByLatlng(oLatlng);
        }
    }

    public void getAddressByLatlng(LatLng latLng) {
        //逆地理编码查询条件：逆地理编码查询的地理坐标点、查询范围、坐标类型。
        LatLonPoint latLonPoint = new LatLonPoint(latLng.latitude, latLng.longitude);
        RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 500f, GeocodeSearch.AMAP);
        //异步查询
        geocodeSearch.getFromLocationAsyn(query);
        LogUtil.e("tag", "异步查询位置》》》》》》》》》》》》" + latLng.toString());
    }


    interface MapLocatino {
        void onLocationChanged(String addresss);
    }
}
